package com.codeoss.framework.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CustomExceptionHandler {
	@ExceptionHandler(Exception.class)
	public void exceptionHander(Exception exception,HttpServletResponse httpServletResponse)throws IOException{
		httpServletResponse.setContentType("application/json");
		httpServletResponse.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = httpServletResponse.getWriter();
		// String message=e.getMessage().split(":")[1];
		Throwable preException = exception;
		Throwable root = exception;
		while (true) {
			preException = preException.getCause();
			if (preException == null) {
				break;
			}
			root = preException;
		}
		exception.printStackTrace();
		printWriter.write("{\"code\":0,\"message\":\"" + root.getMessage() + "\"}");
	}
}
